package mathFactsGUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Date;

import javax.swing.*;
import javax.swing.border.Border;

import results.IncorrectProblemStatistic;
import results.Result;

import databaseComm.DatabaseAccessAPIStub;
import databaseComm.DatabaseDriverInterface;

import equations.AdditionStub;
import equations.DivisionStub;
import equations.FractionToDecimalStub;
import equations.Multiplication1Stub;
import equations.Multiplication2Stub;
import equations.SubtractionStub;

import accounts.Account;
import accounts.Student;
import accounts.Admin;
import accounts.Instructor;

/**
 * Creates a Graphic User Interface main menu for the Kumon MathFacts program
 * @author stewarte
 *
 */
public class GUI extends JFrame {
	
	/**
	 * This is the reference to the database access, we only want one instance of this
	 * accross the whole program
	 */
	private static DatabaseDriverInterface databaseAccess;
	
	/**
	 * TODO: Add url of database here
	 */
	private String databaseURL = "";
	
	/**
	 * app ID
	 */
	private static final long serialVersionUID = 7224310565796646304L;
	
	/**
	 * Array for holding GUI buttons that can be enabled or disabled, specifically, the test category buttons
	 */
	protected JButton[] buttons= null;
	
	/**
	 * Textfield for logging in with a given username
	 */
	protected JTextField usernameTextfield=null;
	
	/**
	 * textfield for user password
	 */
	protected JTextField passwordTextfield=null;
	
	/**
	 * Enter button for entering username and password information
	 */
	private JButton enterLoginInfo=null;
	
	/**
	 * JButton to activate hte staff login
	 */
	private JButton staffLogin=null;
	
	/**
	 * Button to logout the user
	 */
	private JButton logOut=null;
	
	/**
	 * Button for creating a new user
	 */
	private JButton newUser=null;
	
	/**
	 * Name of the currently logged in user
	 */
	protected String logInName=null;
	
	/**
	 * Student object
	 */
	protected static Account activeAccount=null;
	
	/**
	 * The temporary name 
	 */
	protected String tempName=null;
	
	/**
	 * This color corresponds with kumon's blue color
	 */
	protected final Color kumonBlue= new Color(103,238,255);
	
	/**
	 * This color will correspond to each of the button's colors, ultimately defines their gradient
	 */
	protected final Color buttonBlue = new Color(0,162,232);
	
	/**
	 * This is the font used whenever we want arial text
	 */
	protected final Font arial=new Font("Arial", Font.PLAIN, 26);
	
	/**
	 * This is the default black border wherever a border is used
	 */
	private Border blackBoarder = BorderFactory.createLineBorder(Color.BLACK, 4);
	
	/**
	 * This is the border used universally for buttons
	 */
	private Border buttonBorder = BorderFactory.createLineBorder(Color.BLACK, 2);
	
	/**
	 * This is the jframe representing a new user.
	 */
	protected JFrame newUserFrame;
	
	
	/**
	 * Creates the main menu
	 */
	public  GUI(){
		
		/**
		 * For future release, 
		 * don't change now in prototype
		 * , we need to make our layouts liquid, no fixed dimensions,
		 *  calculate pixels based on screen size.
		 */
		// Set up our window
		this.setTitle("Kumon Math Facts");
		//set window dimensions fixed dimensions for prototype
		this.setSize(1024,728);
		//closing GUI is disabled, we don't want kids to just exit
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		//fixed size for prototype
		this.setResizable(false);
		//this.setUndecorated(true);
		this.setLocationRelativeTo(null);
		
		this.setUndecorated(true);
		
		//initialize the database
		databaseAccess = new DatabaseAccessAPIStub(databaseURL);
		
		//Initialize the background
		JPanel backdrop = new JPanel();
		ImageIcon backgroundImage = new ImageIcon("images\\kumon_app_background.png");
		JLabel backgroundComp = new JLabel(backgroundImage);
		backgroundComp.setBounds(0,0,backgroundImage.getIconWidth(),backgroundImage.getIconHeight());
		this.getLayeredPane().add(backgroundComp,new Integer(Integer.MIN_VALUE));
		backdrop.setOpaque(false);
		this.setContentPane(backdrop);	
		backdrop.setBorder(buttonBorder);
		
		
		// Creates components to for the main window
		ImageIcon kumonHeader = new ImageIcon("images\\header.png");
		JLabel kumonLogo= new JLabel(kumonHeader);
		kumonLogo.setBorder(blackBoarder);
		
		JPanel north = new JPanel();
		north.setOpaque(false);
		JPanel center=new JPanel();
		center.setOpaque(false);
		JPanel west= new JPanel();
		//west.setOpaque(false);
		center.setLayout(new GridLayout(0,2, 20, 20));
		west.setLayout(new GridBagLayout());
		
		//Creates components for the center panel
		JButton addition= new JButton("Addition");
		addition.addActionListener(new AL());
		addition.setBorder(buttonBorder);
		JButton subtraction= new JButton("Subtraction");
		subtraction.addActionListener(new AL());
		subtraction.setBorder(buttonBorder);
		JButton multiplication1= new JButton("Multiplication I");
		multiplication1.addActionListener(new AL());
		multiplication1.setBorder(buttonBorder);
		JButton multiplication2= new JButton("Multiplication II");
		multiplication2.addActionListener(new AL());
		multiplication2.setBorder(buttonBorder);
		JButton division = new JButton("Division");
		division.addActionListener(new AL());
		division.setBorder(buttonBorder);
		JButton decFrac = new JButton("Fractions to Decimals");
		decFrac.addActionListener(new AL());
		decFrac.setBorder(buttonBorder);

		GridBagConstraints c= new GridBagConstraints();
		
		//Creates components for the West Panel
		c.anchor=(GridBagConstraints.FIRST_LINE_START);
		c.insets= new Insets(0,10,5,0);

		
		JLabel login = new JLabel("Username: ");
		
		c.gridx=0;
		c.gridy=1;
		
		west.add(login,c);
		
		
		usernameTextfield= new JTextField(9);
		
		c.gridx=0;
		c.gridy=2;
		c.ipadx=0;
		c.insets= new Insets(0,10,5,10);
		
		west.add(usernameTextfield,c);
		
		JLabel passwordLine = new JLabel("Password:");
		c.gridy=3;
		west.add(passwordLine,c);
		
		passwordTextfield = new JPasswordField(9);
		c.gridx=0;
		c.gridy=4;
		c.ipadx=0;
		c.insets = new Insets(0,10,5,5);
		west.add(passwordTextfield,c);
		
		enterLoginInfo = new JButton("Login");
		
		c.ipadx=35;
		c.gridx=0;
		c.gridy=5;
		enterLoginInfo.addActionListener(new AL());
		
		west.add(enterLoginInfo,c);
		
		logOut = new JButton("Log Out");
		
		logOut.addActionListener(new AL());
		c.ipadx=22;
		c.gridx=0;
		c.gridy=6;
		
		west.add(logOut,c);
		logOut.setEnabled(false);
		
		staffLogin= new JButton("Staff Tools");
		staffLogin.addActionListener(new AL());
		staffLogin.setMargin(new Insets(3,14,2,14)); 
		staffLogin.setEnabled(false);
		
		c.ipadx=5;
		c.gridx=0;
		c.gridy=7;
		
		west.add(staffLogin,c);
		
		JTextArea blankarea= new JTextArea();
		blankarea.setSize(20,100);
		blankarea.setEnabled(false);
		blankarea.setOpaque(false);
		c.ipady=385;
		c.gridx=0;
		c.gridy=7;
		
		west.add(blankarea,c);
		west.setBackground(kumonBlue);
		
		//Creates a container for all JButton components
		buttons= new JButton[]{addition, subtraction, multiplication1, multiplication2, division, decFrac};
		
		//Deactivates buttons that should not be active
		enableButtons(false);
		
		//Adds components to the center panel
		center.add(addition);
		center.add(subtraction);
		center.add(multiplication1);
		center.add(multiplication2);
		center.add(division);
		center.add(decFrac);
		
		
		north.add(kumonLogo);
		west.setOpaque(false);
		north.setBackground(kumonBlue);
		
		
		//Adds components to main window
		backdrop.setLayout(new BorderLayout());
		backdrop.add(north,BorderLayout.NORTH);
		backdrop.add(center, BorderLayout.CENTER);
		backdrop.add(west, BorderLayout.WEST);
		
	
		//this.add(backdrop);
		this.setVisible(true);
		
		
	}//End GUI constructor
	
	/**
	 * Enables all buttons or disables depending on Boolean Input
	 * @param enable
	 */
	public void enableButtons(Boolean enable){
		
		if (enable ==false){
			
			for(int i =0; i<buttons.length; i++){
				buttons[i].setEnabled(false);
				buttons[i].setFont(arial);
				buttons[i].revalidate();
				}//end for
		
		}else if (enable){
			
			for(int i =0; i<buttons.length; i++){
				
				buttons[i].setEnabled(true);
				buttons[i].setForeground(Color.black);
				buttons[i].setFont(arial);
				buttons[i].revalidate();
				}//end for
			
		}//end if enable
		
		
	}//end Enable Buttons
	
	/**
	 * Method that get's the time in the format of year/month/day hours:minutes:seconds (military time)
	 * @return String Date
	 */
    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    /**
     * Validates the log in information
     * @param username
     * @param password
     * @return
     */
    private boolean loginSearch(String username, String password){
    	//flag for login success
    	boolean success = false;
    	
    	//assign the active account
    	activeAccount = databaseAccess.authenticateLogin(username,password);
    		
    	if(activeAccount!=null && activeAccount instanceof Admin){
    		success=true;
    	}else if(activeAccount!=null && activeAccount instanceof Instructor){
    		success=true;
    	}else if(activeAccount!=null && activeAccount instanceof Student){
    		success=true;
    	}
    	return success;
    	
    }
	
	/**
	 * Action Listener for the buttons in the GUI
	 * @author stewarte
	 *
	 */
	private class AL implements ActionListener{

		
		//GUI Components for the new entry GUI
		private JTextField usernameEntry;
		private JTextField passwordEntry;
		private JTextField firstNameEntry;
		private JTextField lastNameEntry;
		private JTextField retypeEntry;
		private JLabel username;
		private JLabel passwordLabel;
		private JLabel retypePassword;
		private JLabel firstName;
		private JLabel lastName;
		private JComboBox accounts;

		@Override
		public void actionPerformed(ActionEvent ae) {
			if(ae.getSource() instanceof JButton){
				//Enter button
				if(ae.getActionCommand()=="Login"){
					//gets the username from the user and assigns it to tempname
					tempName=usernameTextfield.getText();
					logInName=tempName;
					
					//if a valid log in name
					if(loginSearch(tempName,passwordTextfield.getText())){
						
						//ActiveAccount should be set now
						if(activeAccount instanceof Admin || activeAccount instanceof Instructor){
							staffLogin.setEnabled(true);
						}//End if Admin
						else if(activeAccount instanceof Student){
							//enable the test buttons if student
							enableButtons(true);
						}
						//set logout button to be enabled
						logOut.setEnabled(true);
						enterLoginInfo.setEnabled(false);
						

					}//End if confirmLogin
					
					//if entry is empty
					else if(tempName.length()==0){
						JOptionPane.showMessageDialog(null,"Please type in your name before pressing login");
					}//if the login information does not match and is not empty
					else{
						JOptionPane.showMessageDialog(null, "Login information is invalid. Please create a new account");
					}
					

					
					
				}//End if button enter
				
				//Log Out button
				
				//if the user logs out, resets the name box, disables all buttons, reenables the enter button
				if(ae.getActionCommand()=="Log Out"){
					//reset everything
					usernameTextfield.setText("");
					passwordTextfield.setText("");
					enableButtons(false);
					enterLoginInfo.setEnabled(true);
					logOut.setEnabled(false);
					staffLogin.setEnabled(false);
					activeAccount=null;
				}
				
				//Multiplication I Button
				if(ae.getActionCommand()=="Multiplication I"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer multiplication = new Quizzer(databaseAccess.getAllEquationsForCategory("Multiplication I"));
					//sql logic is handled from quizzer here
				}//End if Multiplication I
				
				//Multiplication II Button
				if(ae.getActionCommand()=="Multiplication II"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer multiplication2 = new Quizzer(databaseAccess.getAllEquationsForCategory("Multiplication II"));

				}//End if Multiplication II
				
				//Addition Button
				if(ae.getActionCommand()=="Addition"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer addition = new Quizzer(databaseAccess.getAllEquationsForCategory("Addition"));
					
				}//End if Addition
				
				//Subtraction Button
				if(ae.getActionCommand()=="Subtraction"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer subtraction = new Quizzer(databaseAccess.getAllEquationsForCategory("Subtraction"));
				}//End if Subtraction
				
				//Division Button
				if(ae.getActionCommand()=="Division"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer division = new Quizzer(databaseAccess.getAllEquationsForCategory("Division"));

				}//End if Division
				
				//Fractions to Decimals button
				if(ae.getActionCommand()=="Fractions to Decimals"){
					enableButtons(false);
					@SuppressWarnings("unused")
					Quizzer fracToDec = new Quizzer(databaseAccess.getAllEquationsForCategory("Fractions to Decimals"));

				}//End if Fractions to Decimals
				
				if(ae.getActionCommand()=="Staff Tools"){
					JFrame staffTools= new JFrame();
					staffTools.setLocation(700,100);
					staffTools.setSize(400,600);
					staffTools.setVisible(true);
					staffTools.setDefaultCloseOperation(EXIT_ON_CLOSE);
					staffTools.setTitle("Staff Tools");
					staffTools.setLayout(new BorderLayout(3,3));
					
					//we check if the active account is an admin
					boolean isAdmin = activeAccount instanceof Admin;
					
					
					JPanel centerPanel = new JPanel();
					centerPanel.setLayout(new GridLayout(0,1));
					
					//only an admin can create a new user account
					newUser = new JButton("New User");
					newUser.addActionListener(new AL());
					
					if(!isAdmin){
						newUser.setEnabled(false);
					}
					
					//implement the delete user option
					JButton deleteUser = new JButton("Delete User");
					deleteUser.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							//Get the username of the account to be deleted
							String accountToBeDeleted = JOptionPane.showInputDialog(null, "Type in " +
									"the name of the account you wish to delete.");
							//delete the account
							boolean deleted =databaseAccess.deleteAccount(activeAccount,accountToBeDeleted);
							
							if(deleted){
								JOptionPane.showMessageDialog(null, "Account "+accountToBeDeleted+" successfully deleted.");
							}else{
								JOptionPane.showMessageDialog(null, "Account could not be deleted.");
							}
							
						}
						
					});
					
					//only an admin can delete a user
					if(!isAdmin){
						deleteUser.setEnabled(false);
					}
					
					//Implement the changePassword Option
					JButton changePassword = new JButton("Change Password");
					changePassword.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							//Get the username of the account to be deleted
							String changePasswordUsername = JOptionPane.showInputDialog(null, "Type in " +
									"the account username whose password needs changed.");
							
							//delete the account
							boolean availableUsername = databaseAccess.usernameAvailable(changePasswordUsername);
							
							if(availableUsername){
								String newPassword = JOptionPane.showInputDialog(null, "Type in the new Password.");
								String confirmPassword = JOptionPane.showInputDialog(null, "Confirm the new Password.");
								if(newPassword.equals(confirmPassword)){
									boolean result = databaseAccess.changePassword(activeAccount,changePasswordUsername, newPassword);
									if(result){
										JOptionPane.showMessageDialog(null,"Password changed successfully.");
									}else{
										JOptionPane.showMessageDialog(null,"Password change failed.");
									}
								}else{
									JOptionPane.showMessageDialog(null, "Passwords did not match, please try again");
								}
							}else{
								JOptionPane.showMessageDialog(null, "Username does not exist in database.");
							}
							
						}
						
					});
					
					//only an admin can change a password
					if(!isAdmin){
						changePassword.setEnabled(false);
					}
					
					
					JButton retrievePassword = new JButton("Retrieve Password");
					retrievePassword.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							//Get the username of the account to be deleted
							String retrieveUsername = JOptionPane.showInputDialog(null, "Type in " +
									"the account username whose password need's retrieved.");
							
							//delete the account
							boolean availableUsername = databaseAccess.usernameAvailable(retrieveUsername);
							
							if(availableUsername){
								String retrievedPassword = databaseAccess.retrievePassword(activeAccount,retrieveUsername);
								JOptionPane.showMessageDialog(null, "The password for account "+retrieveUsername+" is "+retrievedPassword);
								
							}else{
								JOptionPane.showMessageDialog(null, "Username does not exist in database.");
							}
							
						}
						
					});
					
					//anyone can retrieve a password
					
					JButton trackStudentProgress = new JButton("Track Student Progress");
					trackStudentProgress.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							String studUserName = JOptionPane.showInputDialog(null, "Type in the username of the student whose report you wish to show");
							
							if(databaseAccess.usernameAvailable(studUserName)){
							//get all results for the student
							List<Result> results = databaseAccess.getAllResults(studUserName);
							List<IncorrectProblemStatistic> problems = databaseAccess.getIncorrectProblemsStatistics(studUserName);
							//we have the results and we have the statistics about incorrect problems
							//TODO: Use these in a new GUI, temporarily just implemented with JOption Pane
							String resultsString ="";
							for(Result r: results){
								resultsString=resultsString+"\n"+r.getResultSummary();
							}
							JOptionPane.showMessageDialog(null, resultsString);
							}else{
								JOptionPane.showMessageDialog(null,"User does not exist");
							}
						}
					
					});
					
					//anyone can track student progress
					
					centerPanel.add(newUser);
					centerPanel.add(deleteUser);
					centerPanel.add(changePassword);
					centerPanel.add(retrievePassword);
					centerPanel.add(trackStudentProgress);
					
					JButton exit= new JButton("Exit");
					exit.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent arg0) {
							System.exit(0) ;
						}
					});
					staffTools.add(centerPanel, BorderLayout.CENTER);
					staffTools.add(exit,BorderLayout.SOUTH);
				}
				if(ae.getActionCommand()=="New User"){
					
					newUserFrame = new JFrame();
					newUserFrame.setSize(350,460);
					
					JPanel mainPanel = new JPanel();
					JPanel topPanel = new JPanel();
					JPanel backPanel = new JPanel();
					
					backPanel.setLayout(new BorderLayout());
					topPanel.setLayout(new GridBagLayout());
					mainPanel.setLayout(new GridBagLayout());
					
					GridBagConstraints c = new GridBagConstraints();
					c.anchor=GridBagConstraints.PAGE_START;
					
					//Gives directions on correct username
					JLabel directions = new JLabel("Let's create an account, Please enter your");
					c.gridx=0;
					c.gridy=0;
					topPanel.add(directions,c);
					
					JLabel directions2 = new JLabel("last name with the first initial of");
					c.gridx=0;
					c.gridy=1;
					topPanel.add(directions2,c);
					
					JLabel directions3 = new JLabel("your first name as your username.");
					c.gridy=2;
					topPanel.add(directions3,c);
					
					JLabel blankLine = new JLabel(" ");
					c.gridy=3;
					topPanel.add(blankLine, c);
					
					JLabel directions4 = new JLabel("Next, choose any password that you like.");
					c.gridy=4;
					topPanel.add(directions4,c);
					
					JLabel blankLine2 = new JLabel(" ");
					c.gridy=5;
					topPanel.add(blankLine2,c);
					
					JLabel directions5 = new JLabel("Finally, enter your first and last name");
					c.gridy=6;
					topPanel.add(directions5,c);
					
					JLabel directions6 = new JLabel("and then press the 'Create Account' button.");
					c.gridy=7;
					topPanel.add(directions6,c);
					
					username = new JLabel("Username:  ");
					c.anchor=GridBagConstraints.NORTHWEST;
					c.gridx=0;
					c.gridy=2;
					c.insets=new Insets(20,5,0,5);
					c.weightx=.1;
					mainPanel.add(username,c);
					
					usernameEntry = new JTextField(9);
					c.gridx=1;
					c.weightx=.5;
					c.ipadx=15;
					mainPanel.add(usernameEntry,c);
					
					passwordLabel = new JLabel("Password: ");
					c.gridx =0;
					c.gridy=3;
					mainPanel.add(passwordLabel,c);
					
					passwordEntry = new JPasswordField(9);
					c.gridx=1;
					
					mainPanel.add(passwordEntry,c);
					
					retypePassword = new JLabel("Retype password: ");
					c.gridx=0;
					c.gridy=4;
					mainPanel.add(retypePassword, c);
					
					retypeEntry = new JPasswordField(9);
					c.gridx=1;
					mainPanel.add(retypeEntry,c);
					
					
					firstName = new JLabel("First Name: ");
					c.gridx =0;
					c.gridy=5;
					mainPanel.add(firstName,c);
					
					firstNameEntry = new JTextField(9);
					c.gridx = 1;
					mainPanel.add(firstNameEntry,c);
					
					lastName = new JLabel("Last Name: ");
					c.gridx=0;
					c.gridy=6;
					mainPanel.add(lastName,c);
					
					lastNameEntry = new JTextField(9);
					c.gridx=1;
					c.gridy=6;
					mainPanel.add(lastNameEntry, c);
					
					JLabel accountLabel = new JLabel("Account Type:");
					c.gridx=0;
					c.gridy=7;
					mainPanel.add(accountLabel,c);
					
					String[] accountTypes = { "Student", "Instructor", "Admin" };
					accounts = new JComboBox(accountTypes);
					c.gridx=1;
					c.gridy=7;
					mainPanel.add(accounts,c);
					
					JButton createAccount = new JButton("Create Account");
					
					c.gridx=0;
					c.gridy=8;
					mainPanel.add(createAccount,c);
					
					JButton cancel = new JButton("Cancel");
					c.gridx=1;
					c.gridy=8;
					mainPanel.add(cancel,c);
					
					
					backPanel.add(topPanel,BorderLayout.NORTH);
					backPanel.add(mainPanel,BorderLayout.CENTER);
					
					
					ActionListener al = new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent ae) {
							if(ae.getActionCommand()=="Create Account"){
								boolean validUserName=databaseAccess.usernameAvailable(usernameEntry.getText());
								
								//if the username is empty
								if(usernameEntry.getText().equals(null)||usernameEntry.getText().equals("")){
									JOptionPane.showMessageDialog(null, "Please type in a user name");
									username.setForeground(Color.red);
									username.setText("*Username: ");
								}
								
								//in the event that the username has already been used
								else if(validUserName==false){
									JOptionPane.showMessageDialog(null,"Username invalid, there is already an account with that name");
								}
								
								//if the password is empty
								else if(passwordEntry.getText().equals(null)||passwordEntry.getText().equals("")){
									JOptionPane.showMessageDialog(null, "Please type in a password");
									passwordLabel.setForeground(Color.red);
									passwordLabel.setText("*Password: ");
									username.setForeground(Color.black);
									username.setText("Username: ");
								}
								
								//if the retype is empty
								else if(retypeEntry.getText().equals(null)||retypeEntry.getText().equals("")){
									JOptionPane.showMessageDialog(null, "Please retype your password");
									retypePassword.setForeground(Color.red);
									retypePassword.setText("*Retype Password: ");
									passwordLabel.setForeground(Color.black);
									passwordLabel.setText("Password: ");
									}
								
								//if the first name is empty
								else if(firstNameEntry.getText().equals(null)||firstNameEntry.getText().equals("")){
									JOptionPane.showMessageDialog(null, "Please type in your first name");
									firstName.setForeground(Color.red);
									firstName.setText("*First Name: ");
									retypePassword.setForeground(Color.black);
									retypePassword.setText("Retype Password: ");	
								}
								
								//if the last name is empty
								else if(lastNameEntry.getText().equals(null)||lastNameEntry.getText().equals("")){
									JOptionPane.showMessageDialog(null, "Please type in your last name");
									lastName.setForeground(Color.red);
									lastName.setText("*Last Name: ");
									firstName.setForeground(Color.black);
									firstName.setText("First Name: ");
								}
								//if the passwords do not match
								else if(!(passwordEntry.getText().equals(retypeEntry.getText()))){
									JOptionPane.showMessageDialog(null, "Your entries do not match, please make sure that you are typing them in correctly");
								}
								//if all form conditions are met, proceeds with adding the student
								else{
								
								String userNameString = usernameEntry.getText();
								String passwordString = passwordEntry.getText();
								String firstNameString = firstNameEntry.getText();
								String lastNameString = lastNameEntry.getText();
								String accountType = (String)accounts.getSelectedItem();
								
								Account newAcc=null;
								
								if(accountType.equals("Student")){
									//create a student
									newAcc= new Student(userNameString,passwordString,firstNameString,lastNameString);
									databaseAccess.createAccount(activeAccount,newAcc);
								}else if(accountType.equals("Admin")){
									//create a student
									newAcc= new Admin(userNameString,passwordString,firstNameString,lastNameString);
									databaseAccess.createAccount(activeAccount,newAcc);
								}else if(accountType.equals("Instructor")){
									//create a student
									newAcc= new Instructor(userNameString,passwordString,firstNameString,lastNameString);
									databaseAccess.createAccount(activeAccount,newAcc);
								}
								
								
								newUserFrame.dispose();
							}//end else everything is ok
								
							}else if(ae.getActionCommand()=="Cancel"){
								newUserFrame.dispose();
							}//end else if action command
							
						}//end action performed
						
					};
					cancel.addActionListener(al);
					createAccount.addActionListener(al);
					
					newUserFrame.add(backPanel);
					newUserFrame.setLocationRelativeTo(null);
					newUserFrame.setVisible(true);
					
				}

			}//End if source JButton
			
		}//End ActionPerformed
		
	}//End ActionListener
	
	/**
	 * We want the database driver to be accessible to all GUI components
	 * @return
	 */
	protected static DatabaseDriverInterface getDBDriverInterface(){
		return databaseAccess;
	}


}//End GUI Class
